package com.ruiao.tools.menjin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.ruiao.tools.R;
import com.ruiao.tools.url.URLConstants;
import com.ruiao.tools.utils.HttpUtil;
import com.ruiao.tools.utils.SPUtils;
import com.ruiao.tools.utils.StatusBarUtil;
import com.ruiao.tools.utils.ToastHelper;
import com.ruiao.tools.widget.Pbdialog;

import cz.msebera.android.httpclient.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MenjinResultActivity extends AppCompatActivity {
    private Pbdialog dialog; //进度条
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    initStatue();
                }
            });
        }
    };
    private Timer mTimer = new Timer();
    public String mn;
    Context context;
    @BindView(R.id.dev_name)
    TextView devName;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_bianhao)
    TextView tvBianhao;
    @BindView(R.id.tv_menjinzhuangtai)
    TextView tvMenjinzhuangtai;
    @BindView(R.id.tv_kaiqizhuangtai)
    TextView tvKaiqizhuangtai;
    @BindView(R.id.img_menjing)
    ImageView imgMenjing;
    @BindView(R.id.open)
    ImageView open;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mn = getIntent().getStringExtra("mn");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menjin_result);
        ButterKnife.bind(this);
        StatusBarUtil.darkMode(this);
        context = this;
        initStatue();
    }

    private void initStatue() {
        RequestParams pa = new RequestParams();
        pa.put("username", SPUtils.get(context, "username", ""));
        pa.add("MN", mn);
        HttpUtil.get(URLConstants.MENJIN, pa, new HttpUtil.SimpJsonHandle(context) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                try {
                    boolean success = response.getBoolean("success");
                    if (dialog != null && dialog.isShowing()) {
                        dialog.dismiss();
                    }
                    if (success) {
                        JSONObject info = response.getJSONObject("data");
                        devName.setText(info.getString("name"));
                        tvCompany.setText(info.getString("company"));
                        tvBianhao.setText(info.getString("number"));
                        tvMenjinzhuangtai.setText(info.getString("statue"));
                        String s1 = info.getString("statue");
                        tvKaiqizhuangtai.setText(info.getString("open"));  //开启状态
                        if (s1.equals("未在线")) {
                            imgMenjing.setImageResource(R.drawable.suoweizaixian);
                        }
                        String ss = tvKaiqizhuangtai.getText().toString();
                        if (ss.equals("打开")) {

                            imgMenjing.setImageResource(R.drawable.suokaiqi);
                        } else if (ss.equals("关闭")) {
                            imgMenjing.setImageResource(R.drawable.suoguanbi);
                        } else if (ss.equals("打开 长时间未关闭")) {
                            imgMenjing.setImageResource(R.drawable.suochanqiweikai);
                        } else if (ss.equals("操作异常")) {
                            imgMenjing.setImageResource(R.drawable.suocaozuoyichang);
                        }
                    } else {
                        ToastHelper.shortToast(context, response.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //    @OnClick(R.id.open)
//    public void onViewClicked() {
//        String ss = tvKaiqizhuangtai.getText().toString();
//        if(!ss.equals("关闭")){
//            ToastHelper.shortToast(context,"当前无法开锁");
//            return;
//        }
//        dialog = showdialog(this, "正在开锁.......");
//
//        RequestParams pa = new RequestParams();
//        pa.put("username", SPUtils.get(context, "username", ""));
//        pa.add("MN", mn);
//        HttpUtil.get(URLConstants.MENJIN_OPEN, pa, new HttpUtil.SimpJsonHandle(context) {
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
//                super.onSuccess(statusCode, headers, response);
//                try {
//                    boolean success = response.getBoolean("success");
//                    if (success) {
//                        mTimer.schedule(timerTask,0,3000);
//                    } else {
//                        ToastHelper.shortToast(context, response.getString("msg"));
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//
//                }
//            }
//        });
//
//
//
//    }
    public Pbdialog showdialog(Context context, String msg) {
        Pbdialog pbdialog = new Pbdialog(context);
        pbdialog.setMessage(msg);
        pbdialog.show();
        return pbdialog;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @OnClick({R.id.open, R.id.ble_open})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.open:
                String ss = tvKaiqizhuangtai.getText().toString();
                if(!ss.equals("关闭")){
                    ToastHelper.shortToast(context,"当前无法开锁");
                    return;
                }
                dialog = showdialog(this, "正在开锁.......");

                RequestParams pa = new RequestParams();
                pa.put("username", SPUtils.get(context, "username", ""));
                pa.add("MN", mn);
                HttpUtil.get(URLConstants.MENJIN_OPEN, pa, new HttpUtil.SimpJsonHandle(context) {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        try {
                            boolean success = response.getBoolean("success");
                            if (success) {
                                mTimer.schedule(timerTask,0,3000);
                            } else {
                                ToastHelper.shortToast(context, response.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();

                        }
                    }
                });
                break;
            case R.id.ble_open:
                Intent intent = new Intent(context,ScanActivity.class);
                intent.putExtra("MN",mn);
                startActivity(intent);
                break;
        }
    }
}
